import router from "@/router/index.ts";
import {ref} from "vue";
import { setRouteEmitter } from '@/router/route-listener.ts'

const whiteList = ['/login', '/error', '/401', '/404', '/403']
const loginPath = '/login'

router.beforeEach(async(to, from, next) => {
  setRouteEmitter(to)
  const userStore = useUserStore()

  const token = useAuthorization()
  if (!token.value) {
    //如果token不存在就跳转登录页
    console.log("如果token不存在就跳转登录页")
    if (!whiteList.includes(to.path) && !to.path.startsWith('/redirect')) {
      next({
        path: loginPath,
        query: {
          redirect: encodeURIComponent(to.fullPath),
        },
      })
      return
    }
  }
  else {
    if (!userStore.userInfo && !whiteList.includes(to.path) && !to.path.startsWith('/redirect')) {
      try {
        //获取用户信息
        await userStore.getUserInfo()
        // 获取路由菜单的信息
        const currentRoute = await userStore.generateDynamicRoutes()
        console.log("router currentRoute", currentRoute)
        router.addRoute(currentRoute)
        next({
          ...to,
          replace: true,
        })
        return
      } catch (e) {
        /*if (e instanceof AxiosError && e?.response?.status === 401) {
          // 跳转到error页面
          next({
            path: '/401',
          })
        }*/
      }
    } else {
      // 如果当前是登录页面就跳转到首页
      if (to.path === loginPath) {
        next({
          path: '/',
        })
        return
      }
    }
  }
  next()
})


router.afterEach((to) => {
  /*useMetaTitle(to)
  useLoadingCheck()
  useScrollToTop()*/
})
